import React from 'react';
import { connect } from 'dva';
import { Button, Table, Form, Input, Popconfirm, DatePicker, Select, TreeSelect,Menu,Dropdown, Icon, } from 'antd';

import AddOutModal from '../../components/out/AddOutModal';
import SecondOutModal from '../../components/out/SecondOutModal'
import OutViewModal  from '../../components/out/OutViewModal'
import DoOutModal from '../../components/out/DoOutModal'
import EditOutTimeModal from '../../components/out/EditOutTimeModal';
const FormItem = Form.Item;
const Option = Select.Option;
const TreeNode = TreeSelect.TreeNode;
const RangePicker = DatePicker.RangePicker;

function Out({ dispatch, out, loading, form }) {

    const { getFieldDecorator, getFieldsValue, getFieldValue } = form;
    const { modalVisible, modalType, roomList, detailList, assetList } = out


    const columns = [{
        title: '申请单号',
        dataIndex: 'id_',
        key: 'id_',
        width: 120,
    }, {
        title: '所在库房/机房',
        dataIndex: 'roomName',
        key: 'roomName',
        width: 150,
    },{
        title: '指定操作人',
        dataIndex: 'userName',
        key: 'userName',
        width: 80,
    }, {
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        width: 80,
        render: (value, row, index) => {
            return (
                value == 1 ? "未完成" : value == 3 ? "已完成" : ''
            )
        }
    }, {
        title: '创建时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: 120,
    }, {
        title: '备注',
        dataIndex: 'remark',
        key: 'remark',
        width: 150,
    }, {
        title: '操作',
        key: 'operation',
        width: 100,
        render: (text, record) => (
            <span>
                {(record.status == 1) ? (
                    <span>
                    <Dropdown overlay={
                        <Menu>
                            <Menu.Item>
                                <a onClick={doOut.bind(null, record.id_)}>确认出库</a>
                            </Menu.Item>
                            <Menu.Item>
                                <Popconfirm title="确认删除？" onConfirm={deleteHandler.bind(null, record.id_)}>
                                <a>删除</a>
                                </Popconfirm>
                            </Menu.Item>         
                        </Menu>
                        }>
                        <a className="ant-dropdown-link">
                         操作<Icon type="down" />
                        </a>
                    </Dropdown>
                    <span className="ant-divider" />
                    <a onClick={onViewItem.bind(null, record.id_)}>查看</a>
                  </span>
                ) : <a onClick={onViewItem.bind(null, record.id_)}>查看</a>}
            </span>
        ),
    }];

    const pagination = {
        current: out.current,
        pageSize: out.currentSize,
        total: out.total,
        showSizeChanger: true,
        showTotal: (total => '共' + out.total + '条'),
        onShowSizeChange(current, size) {
            dispatch({
                type: 'out/queryList',
                payload: { ...getFieldsValue(), ...{ 'pageNum': current, 'pageSize': size } },
            });
        },
        onChange(current) {
            dispatch({
                type: 'out/queryList',
                payload: { ...getFieldsValue(), ...{ 'pageNum': current, 'pageSize': out.currentSize } },
            });
        },
    };

    function deleteHandler(id) {
        dispatch({
            type: 'out/remove',
            payload: { id: id, search: { ...getFieldsValue(), ...{ 'pageNum': out.current, 'pageSize': out.currentSize } }, },
        });
    }

    const editHandler = (id, values) => {
        dispatch({
            type: 'out/edit',
            payload: { id, ...values },
        });
    }
    const submitHandle = () => {
        dispatch({
            type: 'out/queryList',
            payload: { ...getFieldsValue() },
        });
    }
    function handleClear() {
        form.resetFields();
    }

    //确认报废
    const doOut = (id) => {
        dispatch({
            type: 'out/onDoOutViewItem',
            payload: id,
        })
    }

    const onViewItem = (id) => {
        dispatch({
            type: 'out/onViewItem',
            payload: id,
        })
    }
    const addHandler = () => {
        dispatch({
            type: 'out/onAddViewItem',
            payload: {
                modalType: 'create',
            },
        })
    }
 
    const modalProps = {
        item: out.currentItem,
        visible: modalVisible,
        type: modalType,
        maskClosable: false,
        assetList: assetList,
        userList: out.userList,
        id: out.outId,
        assetType: out.assetTypeTree,
        viewDetailList: out.detailList,
        roomList: out.roomList,
        assetBrandList: out.assetBrandList,
        confirmLoading: loading,
        title: `新增出库单`,
        wrapClassName: 'vertical-center-modal',
        newKey: out.newKey,
        dispatch: dispatch,
        onOk(data) {
            dispatch({
                type: `out/create`,
                payload: data,
            })
        },
        onCancel() {
            dispatch({
                type: 'out/hideModal',
            })
        },
    }

    const secondModalProps = {
        item: out.qryAssetItem,
        visible: out.secondModalVisible,
        assetList: assetList,
        userList: out.userList,
        assetType: out.assetTypeTree,
        detailList: detailList,
        roomList: out.roomList,
        assetBrandList: out.assetBrandList,
        assetModelList:out.assetModelList,
        assetCurrent: out.assetCurrent,
        assetCurrentSize: out.assetCurrentSize,
        assetTotal: out.assetTotal,
        maskClosable: false,
        dispatch: dispatch,
        confirmLoading: loading,
        title: '新增明细',
        wrapClassName: 'vertical-center-modal',
        newKey: out.newKey,
        onCancel() {
            dispatch({
                type: 'out/hideSecondModal',
            })
        },
    }

    const viewModalProps = {
        item: out.viewItem,
        visible: out.viewModalVisible,
        maskClosable: false,
        outDetailList: out.outDetailList,
        confirmLoading: loading,
        title: '查看出库单',
        wrapClassName: 'vertical-center-modal',
        newKey: out.newKey,
        onCancel() {
            dispatch({
                type: 'out/hideViewModal',
            })
        },
    }

    const doOutModal = {
        item: out.doOutViewItem,
        visible: out.doOutModalVisible,
        type: out.doOutModalType,
        maskClosable: false,
        outDetailList: out.outDetailList,
        confirmLoading: loading,
        title: `出库`,
        wrapClassName: 'vertical-center-modal',
        dispatch: dispatch,
        newKey: out.newKey,
    }

    const editOutTimeModal = {
        item: out.outItem,
        visible: out.addOutTimeModalVisible,
        type: out.modalType,
        maskClosable: false,
        confirmLoading: loading,
        title: `编辑出库时间`,
        wrapClassName: 'vertical-center-modal',
        dispatch: dispatch,
        newKey: out.newKey,
        onCancel() {
            dispatch({
                type: 'out/hideAddOutTimeModal',
            })
        },
        addOutTimeHandle(values){
            for(var i =0;i<out.outDetailList.length;i++){
                if(values.outDetailId == out.outDetailList[i].id_){
                    out.outDetailList[i].outTime = values.outTime;
                    out.outDetailList[i].outRemark = values.outRemark;
                }
            }
            dispatch({
                type: 'out/hideAddOutTimeModal',
                payload:out.outDetailList,
            })
        }
    }

    const loopRoom = data => data.map((item) => {
        return <Option key={item.id_ + ''}>{item.name}</Option>
    });
    const loopBrand = data => data.map((item) => {
        return <Option key={item.id_ + ''}>{item.name}</Option>
    });
    const typeloop = data => data.map((item) => {
        if (item.assetType && item.assetType.length) {
            return <TreeNode key={item.id_ + ''} title={item.name} value={item.id_ + ''}>{typeloop(item.assetType)}</TreeNode>;
        }
        return <TreeNode key={item.id_ + ''} title={item.name} value={item.id_ + ''} />;
    });

    return (
        <div style={{ backgroundColor: '#fff' }}>
            <div style={{ minHeight: '100%' }}>
                <div style={{ width: '100%', backgroundColor: '#fff', lineHeight: '40px', paddingLeft: '20px', paddingTop: '10px' }}>
                    <Form layout="inline" >
                        <FormItem>
                            {
                                getFieldDecorator('roomId')(
                                    <Select
                                        showSearch
                                        style={{ width: 200 }}
                                        placeholder="选择库房/机房"
                                        optionFilterProp="children"
                                    >
                                        <Option value="">全部</Option>
                                        {loopRoom(roomList)}

                                    </Select>
                                )
                            }
                        </FormItem>
                        <FormItem>
                            {getFieldDecorator('userName')(
                                <Input type="text" placeholder="操作人" style={{ width: '170px' }} />
                            )}
                        </FormItem>
                        {/* <FormItem>
                            {getFieldDecorator('executeTime')(
                                <RangePicker style={{ width: '200px' }} format="YYYY-MM-DD" />
                            )}
                        </FormItem> */}
                        <FormItem>
                            {getFieldDecorator('status')(
                                <Select
                                    showSearch
                                    style={{ width: 170 }}
                                    placeholder="选择状态"
                                    optionFilterProp="children"
                                >
                                    <Option value="">全部</Option>
                                    <Option value="1">未完成</Option>
                                    <Option value="3">已完成</Option>
                                </Select>
                            )}
                        </FormItem>
                        <FormItem>
                            <Button type="primary" size='large' onClick={submitHandle} className='ant-search-btn'>查询</Button>
                        </FormItem>
                        <FormItem>
                            <Button size="large" htmlType="reset" onClick={handleClear}>清空</Button>
                        </FormItem>
                        <div style={{ marginTop: '-40px', textAlign: 'right', paddingRight: '10px' }}>
                            <FormItem>
                                <Button type="primary" size='large' onClick={addHandler}>新增出库单</Button>
                            </FormItem>
                        </div>
                    </Form>
                </div>
                <div style={{ width: '100%', padding: '20px 30px', }}>
                    <Table columns={columns} rowKey={record => record.id_} loading={loading} dataSource={out.list} pagination={pagination}
                    />
                </div>
            </div>
            <AddOutModal {...modalProps} />
            <SecondOutModal {...secondModalProps} />
            <OutViewModal {...viewModalProps} />
            <DoOutModal {...doOutModal} />
            <EditOutTimeModal {...editOutTimeModal}/>
        </div>
    );
}

function mapStateToProps(state) {
    return { out: state.out, loading: state.loading.models.out };
}

Out = Form.create()(Out);

export default connect(mapStateToProps)(Out);
